﻿using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Singer.Middleware.Redis
{
    public static class RedisServiceExtensions
    {
        public static void AddRedis(this IServiceCollection services, IConfiguration configuration)
        {
            var config = configuration.GetSection(RedisOptions.ConfigKey);
            RedisOptions? options = config.Get<RedisOptions>();
            if (options == null)
                throw new Exception("【Redis】Config was not found.");
            options.Validate();
            services.Configure<RedisOptions>(config);
            services.AddSingleton<IRedisConnectionPool, RedisConnectionPool>();
        }

        public static void AddRedis(this IServiceCollection services, Action<RedisOptions> options)
        {
            services.Configure<RedisOptions>(options);
            services.AddSingleton<IRedisConnectionPool, RedisConnectionPool>();
        }
    }
}
